using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Easy.Geo.Kml;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace KmlGeneratorDemoWeb
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Clear();

            Kml kml = new Kml("On the Fly Document");
            
            NetworkLink link = new NetworkLink("Web Service Link", KBool.True, KBool.True, KBool.True);
            link.Link.Href = GetUrlApplication() + "/WebService1.asmx/BuscaPonto";
            link.RefreshVisibility = KBool.True;
            link.Link.RefreshMode = RefreshMode.OnChange;
            link.Link.ViewRefreshMode = ViewRefreshMode.OnStop;

            kml.AddFeature(link);

            StreamWriter stream = new StreamWriter(Response.OutputStream);

            kml.Build(stream);
            
            Response.ContentType = "application/vnd.google-earth.kml+xml";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("Content-disposition", "attachment;filename=myfile.kml");
            Response.Flush();
            Response.End();
        }

        public string GetUrlApplication()
        {
            if (!HttpContext.Current.Request.IsLocal)
            {
                return "http://" + HttpContext.Current.Request.Url.Authority +
                    HttpContext.Current.Request.ApplicationPath;
            }

            return "http://" + HttpContext.Current.Request.Url.Authority;
        }  
    }
}
